<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <!-- 采购单基本信息 -->
        <a-card :bordered="false" title="采购单信息" style="margin-bottom: 16px">
          <a-descriptions :column="2" size="small">
            <a-descriptions-item label="采购单号">{{ purchaseOrder.purchaseNo }}</a-descriptions-item>
            <a-descriptions-item label="预计到货日期">{{ purchaseOrder.expectedArrivalDate }}</a-descriptions-item>
            <a-descriptions-item label="总金额">¥{{ purchaseOrder.totalAmount ? parseFloat(purchaseOrder.totalAmount).toFixed(2) : '0.00' }}</a-descriptions-item>
          </a-descriptions>
        </a-card>
        
        <!-- 审批结果 -->
        <a-form-item label="审批结果" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-radio-group v-decorator="['approveResult', validatorRules.approveResult]">
            <a-radio :value="1">通过</a-radio>
            <a-radio :value="0">拒绝</a-radio>
          </a-radio-group>
        </a-form-item>
        
        <!-- 审批意见 -->
        <a-form-item label="审批意见" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea
            v-decorator="['approveComment']"
            :rows="4"
            placeholder="请输入审批意见"
          />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { postAction } from '@/api/manage'

export default {
  name: 'PurchaseApproveModal',
  data() {
    return {
      title: '采购单审批',
      visible: false,
      confirmLoading: false,
      labelCol: { span: 4 },
      wrapperCol: { span: 20 },
      form: this.$form.createForm(this),
      validatorRules: {
        approveResult: { rules: [{ required: true, message: '请选择审批结果!' }] }
      },
      purchaseOrder: {},
      url: {
        approve: '/wms/purchase/approve'
      }
    }
  },
  methods: {
    show(record) {
      this.visible = true;
      this.purchaseOrder = Object.assign({}, record);
      
      this.$nextTick(() => {
        this.form.setFieldsValue({
          approveResult: 1, // 默认通过
          approveComment: ''
        });
      });
    },
    handleOk() {
      this.form.validateFields((err, values) => {
        if (!err) {
          this.confirmLoading = true;
          
          // 确保approveResult是数字类型
          const formData = {
            id: this.purchaseOrder.id,
            approveResult: parseInt(values.approveResult),  // 确保是数字类型
            approveComment: values.approveComment || ''
          };
          
          postAction(this.url.approve, formData).then(res => {
            if (res.success) {
              this.$message.success('审批成功!');
              this.handleCancel();
              this.$emit('ok');
            } else {
              this.$message.error(res.message || '审批失败!');
            }
          }).finally(() => {
            this.confirmLoading = false;
          });
        }
      });
    },
    handleCancel() {
      this.visible = false;
      this.purchaseOrder = {};
      this.form.resetFields();
    }
  }
}
</script>

<style scoped>
</style> 